package com.harveyguo.nihonngo.controller;

import com.harveyguo.nihonngo.model.BaseEntity;
import com.harveyguo.nihonngo.model.dto.UserCreateDto;
import com.harveyguo.nihonngo.model.dto.UserDto;
import com.harveyguo.nihonngo.model.mapper.UserMapper;
import com.harveyguo.nihonngo.model.vo.UserVo;
import com.harveyguo.nihonngo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户Controller
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取所有用户
     */
    @GetMapping
    public List<UserVo> getAllUsers() {
        return userService.list().stream().map(userMapper::toVo).collect(Collectors.toList());
    }

    /**
     * 根据ID获取用户
     */
    @GetMapping("/{id}")
    public UserVo getUserById(@PathVariable String id) {
        UserDto dto = userService.findById(id);
        return userMapper.toVo(dto);
    }

    /**
     * 创建用户
     */
    @PostMapping
    public UserVo createUser(@Validated @RequestBody UserCreateDto userDto) {
        UserDto dto = userService.createdUser(userDto);
        return userMapper.toVo(dto);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseEntity<UserDto> login(@Validated @RequestBody UserCreateDto userDto) {
        BaseEntity<UserDto> result = userService.login(userDto);
        System.out.println("用户登录: " + result);
        return result;
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public UserVo updateUser(@PathVariable String id, @Validated @RequestBody UserCreateDto userDto) {
        UserDto dto = userService.updateUser(id, userDto);
        return userMapper.toVo(dto);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable String id) {
        userService.deleteUser(id);
    }

    /**
     * 根据拳击水平查找用户
     */
    @GetMapping("/level/{boxingLevel}")
    public List<UserVo> getUsersByBoxingLevel(@PathVariable String boxingLevel) {
        return userService.findByBoxingLevel(boxingLevel).stream()
                .map(userMapper::toVo)
                .collect(Collectors.toList());
    }

    /**
     * 根据经验年数查找用户
     */
    @GetMapping("/experience/{years}")
    public List<UserVo> getUsersByExperience(@PathVariable Integer years) {
        return userService.findByExperienceYears(years).stream()
                .map(userMapper::toVo)
                .collect(Collectors.toList());
    }

    /**
     * 根据偏好风格查找用户
     */
    @GetMapping("/style/{preferredStyle}")
    public List<UserVo> getUsersByPreferredStyle(@PathVariable String preferredStyle) {
        return userService.findByPreferredStyle(preferredStyle).stream()
                .map(userMapper::toVo)
                .collect(Collectors.toList());
    }

    /**
     * 查找附近的用户
     */
    @GetMapping("/nearby")
    public List<UserVo> getNearbyUsers(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "10.0") Double radius) {
        return userService.findNearbyUsers(latitude, longitude, radius).stream()
                .map(userMapper::toVo)
                .collect(Collectors.toList());
    }
}
